package secretary;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import basic.ClientConsole;
import com.sun.rowset.CachedRowSetImpl;
import flexibility.userData;

public class openNewSemester {

	private Date year = new Date();
	public void openNewSemester(String date, ClientConsole secretaryConnection,
			CachedRowSetImpl crs, userData secretaryData, int semester) {
		/*
		 * this will open new semester
		 */
		switch (semester) {
		case 1:// case of next semester
			secretaryData.deleteAll();// this will get the results of the last
			// semester in database
			secretaryData.addData(10);
			secretaryData.addData(1);
			secretaryConnection.acceptMsg(secretaryData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == false) {
					secretaryData.deleteAll();
					secretaryData.addData(85);
					secretaryData.addData(1);
					secretaryConnection.acceptMsg(secretaryData);
					crs = secretaryConnection.getResults();
					if(crs.next()==false){
						secretaryData.deleteAll();
						secretaryData.addData(11);
						secretaryData.addData(0);
						secretaryData.addData(date);
						secretaryConnection.acceptMsg(secretaryData);
						secretaryConnection.getResults();
					}
					else{
					secretaryData.addData(crs.getString(1)+1);
					secretaryData.addData(date);
					secretaryConnection.acceptMsg(secretaryData);
					secretaryConnection.getResults();
					secretaryData.deleteAll();
					secretaryData.addData(9);
					secretaryData.addData(0);
					try {
						crs.beforeFirst();
						crs.next();
						if (crs.getString(2).equals("A")) {
							secretaryData.addData("B");
							secretaryData.addData(date);
						} else {
							secretaryData.addData("A");
							secretaryData.addData(date);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					secretaryData.addData(year.getYear());
					secretaryConnection.acceptMsg(secretaryData);
					secretaryConnection.getResults();
				}
			} }catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			break;
		case 0:// case of current semester
			secretaryData.deleteAll();// this will get the results of the last
			// semester in database
			secretaryData.addData(20);
			secretaryData.addData(1);
			secretaryConnection.acceptMsg(secretaryData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == false) {
					secretaryData.deleteAll();
					secretaryData.addData(85);
					secretaryData.addData(1);
					secretaryConnection.acceptMsg(secretaryData);
					crs = secretaryConnection.getResults();
					if(crs.next()==false){
						secretaryData.deleteAll();
						secretaryData.addData(86);
						secretaryData.addData(0);
						secretaryData.addData(date);
						secretaryConnection.acceptMsg(secretaryData);
						secretaryConnection.getResults();
					}
					else{
					secretaryData.addData(crs.getString(1)+1);
					secretaryData.addData(date);
					secretaryConnection.acceptMsg(secretaryData);
					secretaryConnection.getResults();
					secretaryData.deleteAll();
					secretaryData.addData(84);
					secretaryData.addData(0);
					try {
						crs.beforeFirst();
						crs.next();
						if (crs.getString(2).equals("A")) {
							secretaryData.addData("B");
							secretaryData.addData(date);
						} else {
							secretaryData.addData("A");
							secretaryData.addData(date);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					secretaryData.addData(year.getYear());
					secretaryConnection.acceptMsg(secretaryData);
					secretaryConnection.getResults();
				}
			} }catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			break;
		}

	}

}
